<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单名称" prop="workOrderName">
        <el-input
            v-model="queryParams.workOrderName"
            placeholder="请输入工单名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="staffId">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入负责人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产小组" prop="productDeptId">
        <el-input
            v-model="queryParams.deptName"
            placeholder="请输入生产小组"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品批次号" prop="productionNo">
        <el-input
            v-model="queryParams.productionNo"
            placeholder="请输入产品批次号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['production:order:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="计划名称" align="center" prop="planName" width="180px"/>
      <el-table-column label="工单名称" align="center" prop="workOrderName" width="180px"/>
      <el-table-column label="负责人" align="center" prop="userName"/>
      <el-table-column label="生产小组" align="center" prop="deptName" width="180px"/>
      <el-table-column label="工单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="production_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="产品数量" align="center" prop="productionCount"/>
      <el-table-column label="产品批次号" align="center" prop="productionNo" width="180"/>
      <el-table-column label="产品生产实际创建时间" align="center" prop="startTime" width="180"/>
      <el-table-column label="产品生产预期开始时间" align="center" prop="planStartTime" width="180"/>
      <el-table-column label="产品生产预期结束时间" align="center" prop="planEndTime" width="180"/>
      <el-table-column label="产品生产实际结束时间" align="center" prop="endTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['production:order:edit']"> 查看详情
          </el-button>
          <el-button v-show="scope.row.status==2" link type="primary" icon="Edit" @click="changeStatus(scope.row)"
                     v-hasPermi="['production:order:remove']">入库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改生产工单对话框 -->
    <div>
      <OrderAdd v-model="open" v-model:open="open" :title="title" :id="id" :ins="ins"/>
    </div>
  </div>


</template>

<script setup name="Order">
import {listOrder, getOrder, delOrder, getPlanList, editStatus} from "@/api/production/order";
import {addInWarehouse} from "@/api/production/inWarehouse";
import OrderAdd from "@/views/production/order/orderAdd.vue";
import {ElMessage} from "element-plus";

const {proxy} = getCurrentInstance();
const {production_order_status} = proxy.useDict("production_order_status");

const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const id = ref(0);
const ins = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planId: null,
    planName: null,
    workOrderName: null,
    staffId: null,
    userName: null,
    productDeptId: null,
    deptName: null,
    status: null,
    productionCount: null,
    productionNo: null,
    startTime: null,
    planStartTime: null,
    planEndTime: null,
    endTime: null,
  }
});
const plan = reactive({
  id: 0,
  status: 0,
  planName: ''
})

const {queryParams, form} = toRefs(data);

//获取计划集合
function getPlans() {
  getPlanList(plan).then(response => {
    data1.value[0].children = response.rows
  })
}

/** 查询生产工单列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

//修改生产工单状态
function changeStatus(row) {
  proxy.$modal.confirm('是否确认入库生产工单编号为"' + row.id + '"的数据项？').then(function () {
    return addInWarehouse({id:row.id});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("入库成功");
  }).catch(() => {
  });
}

/** 新增按钮操作 */
function handleAdd() {
  title.value = "添加生产工单";
  open.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  id.value = row.id
  if (row.status === 0) {
    title.value = "修改生产工单";
  } else {
    title.value = "查看生产工单";
  }
  ins.value++;
  open.value = true;
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('production/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

getList();
getPlans();
</script>